{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-06-04T06:37:05.140001Z",
     "start_time": "2025-06-04T06:37:05.118970Z"
    }
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "import re\n",
    "import pymysql\n",
    "from pymysql.cursors import DictCursor\n",
    "config = {\n",
    "    'host': 'localhost',\n",
    "    'user': 'root',\n",
    "    'password': '12345678',\n",
    "    'db': 'emotion',# 这里填入你想访问的数据库名称\n",
    "    'charset': 'utf8mb4',\n",
    "    'cursorclass': DictCursor# 使用字典游标， 使SELECT的返回值更好处理\n",
    "}\n",
    "# 创建数据库连接对象，接下来通过这个对象来对数据库进行操作\n",
    "conn = pymysql.connect(**config)"
   ]
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "['17.5',\n '18.35',\n '18.02',\n '19',\n '19.79',\n '20.15',\n '19',\n '18.2',\n '18.14',\n '17.9',\n '18',\n '17.5',\n '17.8',\n '17.8',\n '17.91',\n '17.5',\n '17.72',\n '17.65',\n '18',\n '19.2',\n '20.5',\n '19.3',\n '18.83',\n '18.79',\n '18.41',\n '19',\n '18.2',\n '18.05',\n '18.18',\n '18.1',\n '18.6',\n '18.4',\n '18.7',\n '18.41',\n '18.8',\n '18',\n '19.07',\n '18.77',\n '19.98',\n '19',\n '18.89',\n '18.5',\n '18.36',\n '17.8',\n '17.9',\n '18.22',\n '18.26',\n '18.5',\n '19.11',\n '18.5']"
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "with conn.cursor() as cursor:\n",
    "\t# 以查询为例\n",
    "    sql = \"SELECT open FROM stock_k WHERE code = '000001' LIMIT 50\"\n",
    "    # 执行查询语句，将结果保存到游标cursor中\n",
    "    cursor.execute(sql)\n",
    "    # 获取查询结果，数据类型为元组tuple，其中元素是表中的每一行，也是元组格式\n",
    "    result = cursor.fetchall()\n",
    "    # 如果是字符游标，则返回嵌套列表，其中元素表中的每一行，但是字典格式 \n",
    "    open_list = []\n",
    "    for row in result:\n",
    "\t    # print(row['open'])# 逐行输出\n",
    "\t    open_list.append(row['open'])# 逐行输出\n",
    "\t# 释放连接\tconn.close()\n",
    "open_list"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-06-04T06:37:05.146427Z",
     "start_time": "2025-06-04T06:37:05.141260Z"
    }
   },
   "id": "8b34a77d2ceee15d",
   "execution_count": 41
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "['neutral',\n 'positive',\n 'negative',\n 'positive',\n 'negative',\n 'negative',\n 'negative',\n 'positive',\n 'negative',\n 'positive',\n 'positive',\n 'negative',\n 'negative',\n 'negative',\n 'negative',\n 'negative',\n 'negative',\n 'neutral',\n 'negative',\n 'negative',\n 'neutral',\n 'negative',\n 'negative',\n 'negative',\n 'neutral',\n 'negative',\n 'neutral',\n 'negative',\n 'positive',\n 'positive',\n 'negative',\n 'negative',\n 'negative',\n 'negative',\n 'negative',\n 'negative',\n 'negative',\n 'negative',\n 'negative',\n 'positive',\n 'negative',\n 'negative',\n 'negative',\n 'negative',\n 'negative',\n 'neutral',\n 'negative',\n 'negative',\n 'positive',\n 'positive']"
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "with conn.cursor() as cursor:\n",
    "\t# 以查询为例\n",
    "    sql = \"SELECT emo FROM stock_nlp WHERE stock_code = '000001' LIMIT 50\"\n",
    "    # 执行查询语句，将结果保存到游标cursor中\n",
    "    cursor.execute(sql)\n",
    "    # 获取查询结果，数据类型为元组tuple，其中元素是表中的每一行，也是元组格式\n",
    "    result = cursor.fetchall()\n",
    "    # 如果是字符游标，则返回嵌套列表，其中元素表中的每一行，但是字典格式 \n",
    "    emo_list = []\n",
    "    for row in result:\n",
    "\t    # print(row['open'])# 逐行输出\n",
    "\t    emo_list.append(row['emo'])# 逐行输出\n",
    "\t# 释放连接\tconn.close()\n",
    "emo_list"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-06-04T06:37:05.155177Z",
     "start_time": "2025-06-04T06:37:05.146427Z"
    }
   },
   "id": "b833ef63aa633422",
   "execution_count": 42
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "首先，我们需要将收盘价从字符串转换为浮点数，并提取时间序列信息。然后，分析情绪走向并结合技术指标来生成预测区间。\n",
      "\n",
      "### 收盘价数据\n",
      "收盘价：['17.5', '18.35', '18.02', '19', '19.79', '20.15', '19', '18.2', '18.14', '17.9', '18', '17.5', '17.8', '17.8', '17.91', '17.5', '17.72', '17.65', '18', '19.2', '20.5', '19.3', '18.83', '18.79', '18.41', '19', '18.2', '18.05', '18.18', '18.1', '18.6', '18.4', '18.7', '18.41', '18.8', '18', '19.07', '18.77', '19.98', '19', '18.89', '18.5', '18.36', '17.8', '17.9', '18.22', '18.26', '18.5', '19.11', '18.5']\n",
      "\n",
      "### 情绪走向\n",
      "情绪走向：['neutral', 'positive', 'negative', 'positive', 'negative', 'negative', 'negative', 'positive', 'negative', 'positive', 'positive', 'negative', 'negative', 'negative', 'negative', 'negative', 'negative', 'neutral', 'negative', 'negative', 'neutral', 'negative', 'negative', 'negative', 'neutral', 'negative', 'neutral', 'negative', 'positive', 'positive', 'negative', 'negative', 'negative', 'negative', 'negative', 'negative', 'negative', 'negative', 'negative', 'negative', 'negative', 'neutral', 'negative', 'negative', 'positive', 'positive']\n",
      "\n",
      "### 预测股价和概率\n",
      "以下是基于收盘价和情绪走向的预测：\n",
      "\n",
      "```python\n",
      "预测股价: [17.5, 18.0, 17.6, 17.3, 16.8, 16.5, 16.1, 15.7, 15.4, 15.1, 14.8, 14.5, 14.2, 13.9, 13.6, 13.3, 12.9, 12.6, 12.3, 12.0, 11.7, 11.4, 11.1, 10.8, 10.5, 10.2, 9.9, 9.6, 9.3, 9.0, 8.7, 8.4, 8.1, 7.8, 7.5, 7.2, 6.9, 6.6, 6.3, 6.0, 5.7, 5.4, 5.1, 4.8, 4.5, 4.2, 4.0, 3.8, 3.6, 3.4, 3.2, 3.0]\n",
      "预测概率: [80%, 70%, 60%, 50%, 40%, 30%, 20%, 10%, 5%, 3%, 1%, 0%, 0%, 0%, 0%, 0%, 0%, 0%, 0%, 0%, 0%, 0%, 0%, 0%, 0%, 0%, 0%, 0%, 0%, 0%, 0%, 0%, 0%, 0%, 0%, 0%, 0%, 0%, 0%, 0%, 0%, 0%, 0%, 0%, 0%, 0%, 0%, 0%, 0%, 0%, 0%, 0%, 0%, 0%]\n",
      "```\n",
      "\n",
      "### 解释\n",
      "1. **收盘价数据**：将每个收盘价字符串转换为浮点数，用于后续分析。\n",
      "2. **情绪走向分析**：通过情绪走向数组，可以看出投资者情绪的变化趋势。例如，初始时期有较多中性和积极情绪，随后变得消极。\n",
      "3. **预测股价**：基于技术分析和情绪走向，生成了一个逐步下跌的预测区间，最终达到5元以下的价格。\n",
      "4. **概率百分比**：根据情绪强度和价格波动，较高的消极情绪导致较低的上涨概率，且随着时间推移，上升概率逐渐减少。\n",
      "\n",
      "请注意，这只是一个示例预测，不代表实际市场表现。投资者应结合其他信息来源进行综合判断。\n",
      "['17.5', '18.35', '18.02', '19', '19.79', '20.15', '19', '18.2', '18.14', '17.9', '18', '17.5', '17.8', '17.8', '17.91', '17.5', '17.72', '17.65', '18', '19.2', '20.5', '19.3', '18.83', '18.79', '18.41', '19', '18.2', '18.05', '18.18', '18.1', '18.6', '18.4', '18.7', '18.41', '18.8', '18', '19.07', '18.77', '19.98', '19', '18.89', '18.5', '18.36', '17.8', '17.9', '18.22', '18.26', '18.5', '19.11', '18.5', '17.5', '18.0', '17.6', '17.3', '16.8', '16.5', '16.1', '15.7', '15.4', '15.1', '14.8', '14.5', '14.2', '13.9', '13.6', '13.3', '12.9', '12.6', '12.3', '12.0', '11.7', '11.4', '11.1', '10.8', '10.5', '10.2', '9.9', '9.6', '9.3', '9.0', '8.7', '8.4', '8.1', '7.8', '7.5', '7.2', '6.9', '6.6', '6.3', '6.0', '5.7', '5.4', '5.1', '4.8', '4.5', '4.2', '4.0', '3.8', '3.6', '3.4', '3.2', '3.0', '1', '2', '3', '4']\n",
      "['80%', '70%', '60%', '50%', '40%', '30%', '20%', '10%', '5%', '3%', '1%', '0%', '0%', '0%', '0%', '0%', '0%', '0%', '0%', '0%', '0%', '0%', '0%', '0%', '0%', '0%', '0%', '0%', '0%', '0%', '0%', '0%', '0%', '0%', '0%', '0%', '0%', '0%', '0%', '0%', '0%', '0%', '0%', '0%', '0%', '0%', '0%', '0%', '0%', '0%', '0%', '0%', '0%', '0%']\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# 构造 Ollama API 请求\n",
    "ollama_url = \"http://localhost:11434/api/chat\"  # Ollama 默认运行在本地 11434 端口\n",
    "data = {\n",
    "    \"model\": \"deepseek-r1:8b\",  # 模型名称，根据实际情况选择\n",
    "    \"messages\": [\n",
    "        {\"role\":\"system\",\"content\":\"你是中国股票分析大师,请你用中文回答接下来的问题\"},\n",
    "        {\"role\": \"user\", \"content\":\"请你基于我给出的近期股票收盘价和投资者情绪，预测未来50天内的股价走势，并给出预测的概率。请你用Python中列表形式给出：预测股价、预测正确的概率百分比。这是示例：预测股价:[17.4,15.4,13.6,12.5,12.6,12,7,12.7],预测概率:[89%,78%,66%,55%,44%,33%,22%,11%]}。\\n接下来分别是近期股票收盘价和投资者情绪走向。收盘价：\" + str(open_list) + \"\\n情绪走向：\" +str(emo_list)},\n",
    "        # {\"role\": \"ai\", \"content\":\"预测股价:[17.4,15.4,13.6,12.5,12.6,12,7,12.7],预测概率:[89%,78%,66%,55%,44%,33%,22%,11%]}\"},\n",
    "        # {\"role\": \"user\", \"content\":emo_list},\n",
    "    ],\n",
    "    \"stream\": False  # 设置为 False，直接返回完整响应\n",
    "}\n",
    "\n",
    "# 调用 Ollama API\n",
    "\n",
    "response = requests.post(ollama_url, json=data)\n",
    "response.raise_for_status()\n",
    "ollama_response = response.json()\n",
    "assistant_message = ollama_response.get('message', {}).get('content', 'No response from model')\n",
    "# print(assistant_message)\n",
    "assistant_message = re.findall(r'</think>(.*)',assistant_message,re.DOTALL)[0]\n",
    "print(assistant_message)\n",
    "new_session_id = ollama_response.get('session_id')  # 获取新的会话 ID\n",
    "floats = re.findall(r'\\b\\d+(?:\\.\\d+)?\\b(?!%)', assistant_message)\n",
    "percents = re.findall(r'\\b\\d+(?:\\.\\d+)?%', assistant_message)\n",
    "print(floats)\n",
    "print(percents)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-06-04T07:04:14.876728Z",
     "start_time": "2025-06-04T07:03:14.848252Z"
    }
   },
   "id": "af50d6d16a12edfe",
   "execution_count": 56
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['84.2%', '89.8%', '80.0%', '86.0%', '88.2%', '85.7%', '81.6%', '87.1%', '87.2%', '88.2%', '90.0%', '81.6%', '85.6%', '80.5%', '84.0%', '80.5%', '85.8%', '82.1%', '81.8%', '87.9%', '88.8%', '88.0%', '89.5%', '81.1%', '82.6%', '82.8%', '88.1%', '88.8%', '89.9%', '81.3%', '86.1%', '86.6%', '87.6%', '87.1%', '81.0%', '88.2%', '87.7%', '84.0%', '84.5%', '84.8%', '81.2%', '83.4%', '81.8%', '86.8%', '86.1%', '86.5%', '87.5%', '86.9%', '86.7%', '84.1%']\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "\n",
    "# 生成50个随机浮点数，并保留小数点后一位\n",
    "random_floats = [f\"{round(random.uniform(80.0, 90.0), 1)}%\" for _ in range(50)]\n",
    "\n",
    "# 返回列表\n",
    "result = random_floats\n",
    "print(result)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-06-04T11:54:13.649086Z",
     "start_time": "2025-06-04T11:54:13.644453Z"
    }
   },
   "id": "8aac3ce196461967",
   "execution_count": 4
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "ea83e15420a255ee"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
